package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import test.serverframe.armc.server.manager.domain.Tag;
import test.serverframe.armc.server.manager.domain.TaskInvoke;
import test.serverframe.armc.server.manager.domain.params.TagPageParams;

import java.util.List;

/**
 * @author UaenaSA
 */
public interface TagMapper {
    @Delete({
            "delete from tb_ssgp_tag",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @Insert({
            "insert into tb_ssgp_tag (id, tag)",
            "values (#{id,jdbcType=VARCHAR}, #{tag,jdbcType=VARCHAR})"
    })
    int insert(Tag record);

    @InsertProvider(type = TagSqlProvider.class, method = "insertSelective")
    int insertSelective(Tag record);

    @Select({
            "select",
            "id, tag",
            "from tb_ssgp_tag",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "tag", property = "tag", jdbcType = JdbcType.VARCHAR)
    })
    Tag selectByPrimaryKey(String id);

    @UpdateProvider(type = TagSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Tag record);

    @Update({
            "update tb_ssgp_tag",
            "set tag = #{tag,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(Tag record);

    @Select({
            "SELECT" +
                    " tag.id, " +
                    " tag.tag " +
                    " FROM " +
                    " tb_ssgp_tag tag, " +
                    " tb_ssgp_invoke_enter_tag invoke_tag " +
                    " WHERE" +
                    " tag.id = invoke_tag.tag_id " +
                    " AND invoke_tag.invoke_id=#{taskInvokeId,jdbcType=VARCHAR} " +
                    " ORDER BY tag.tag "
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tag", property = "tag", jdbcType = JdbcType.VARCHAR)

    })
    List<Tag> findTagsByTaskInvokeId(String taskInvokeId);

    @Select({
            " SELECT " +
                    " invoke.* " +
                    " FROM" +
                    " tb_ssgp_task_invoke invoke, " +
                    " tb_ssgp_invoke_enter_tag invoke_tag " +
                    " WHERE " +
                    " invoke.task_invoke_id = invoke_tag.invoke_id " +
                    " AND invoke_tag.tag_id=#{tagId,jdbcType=VARCHAR} AND invoke.is_used=1 "
    })
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "class_name", property = "className", jdbcType = JdbcType.VARCHAR),
            @Result(column = "method_name", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params", property = "params", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_cycle", property = "invokeCycle", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_interval", property = "invokeInterval", jdbcType = JdbcType.VARCHAR),
            @Result(column = "start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "cron_expression", property = "cronExpression", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "updata_time", property = "updataTime", jdbcType = JdbcType.TINYINT),

    })
    List<TaskInvoke> findInvokesByTagId(String tagId);

    @SelectProvider(type = TagSqlProvider.class, method = "selectByPage")
    @Results(
            {
                    @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
                    @Result(column = "tag", property = "tag", jdbcType = JdbcType.VARCHAR),

            }

    )
    List<Tag> selectByPage(TagPageParams params);

}